home *** CD-ROM | disk | FTP | other *** search
/ Programmer Plus 2007 / Programmer-Plus-2007.iso / Programming / Borland Plateform / Turbo Pascal V7.0 / DOCDEMO.ZIP / COLLECT4.PAS < prev    next >
Encoding:
Pascal/Delphi Source File  |  1992-10-30  |  5.3 KB  |  216 lines

  1. {************************************************}
  2. {                                                }
  3. {   Turbo Vision 2.0 Demo                        }
  4. {   Copyright (c) 1992 by Borland International  }
  5. {                                                }
  6. {************************************************}
  7.  
  8. { Create a collection of graphical objects: Points, Circles,
  9.   and Rectangles. Use the ForEach iterator to display each
  10.   object in the collection.
  11.  
  12.   If you are running this program in the IDE, be sure to enable
  13.   the full graphics save option when you load TURBO.EXE:
  14.  
  15.     turbo -g
  16.  
  17.   This ensures that the IDE fully swaps video RAM and keeps
  18.   "dustclouds" from appearing on the user screen when in
  19.   graphics mode. You can enable this option permanently
  20.   via the Options|Environment|Startup dialog.
  21.  
  22.   This program uses the Graph unit and its .BGI driver files to
  23.   display graphics on your system. The "PathToDrivers"
  24.   constant defined below is set to \TP\BGI, which is the default
  25.   location of the BGI files as installed by the INSTALL program.
  26.   If you have installed these files in a different location, make
  27.   sure the .BGI file for your system (EGAVGA.BGI, etc.) is in the
  28.   current directory or modify the "PathToDrivers" constant
  29.   accordingly.
  30. }
  31.  
  32. program Collect4;
  33.  
  34. uses
  35.   Objects, Graph;
  36.  
  37. const
  38.   PathToDrivers = '\TP\BGI';  { Default location of *.BGI files }
  39.  
  40. { ********************************** }
  41. { ******  Graphical Objects  ******* }
  42. { ********************************** }
  43.  
  44. type
  45.   PGraphObject = ^TGraphObject;
  46.   TGraphObject = object(TObject)
  47.     X,Y: Integer;
  48.     constructor Init;
  49.     procedure Draw; virtual;
  50.   end;
  51.  
  52.   PGraphPoint = ^TGraphPoint;
  53.   TGraphPoint = object(TGraphObject)
  54.     procedure Draw; virtual;
  55.   end;
  56.  
  57.   PGraphCircle = ^TGraphCircle;
  58.   TGraphCircle = object(TGraphObject)
  59.     Radius: Integer;
  60.     constructor Init;
  61.     procedure Draw; virtual;
  62.   end;
  63.  
  64.   PGraphRect = ^TGraphRect;
  65.   TGraphRect = object(TGraphObject)
  66.     Width, Height: Integer;
  67.     constructor Init;
  68.     procedure Draw; virtual;
  69.   end;
  70.  
  71. { TGraphObject }
  72. constructor TGraphObject.Init;
  73. begin
  74.   X := Random(GetMaxX);
  75.   Y := Random(GetMaxY);
  76. end;
  77.  
  78. procedure TGraphObject.Draw;
  79. begin
  80.   Abstract;     { Give error: This object should never be drawn }
  81. end;
  82.  
  83. { TGraphPoint }
  84. procedure TGraphPoint.Draw;
  85. var
  86.   DX, DY: Integer;
  87. begin
  88.   { Make it a fat point so you can see it }
  89.   for DX := x - 2 to x + 2 do
  90.     for DY := y - 2 to y + 2 do
  91.       PutPixel(DX, DY, 1);
  92. end;
  93.  
  94. { TGraphCircle }
  95. constructor TGraphCircle.Init;
  96. begin
  97.   inherited Init;
  98.   Radius := 20 + Random(20);
  99. end;
  100.  
  101. procedure TGraphCircle.Draw;
  102. begin
  103.   Circle(X, Y, Radius);
  104. end;
  105.  
  106. { TGraphRect }
  107. constructor TGraphRect.Init;
  108. begin
  109.   inherited Init;
  110.   Width := 10 + Random(20) + X;
  111.   Height := 6 + Random(15) + Y;
  112. end;
  113.  
  114. procedure TGraphRect.Draw;
  115. begin
  116.   Rectangle(X, Y, X + Width, Y + Height);
  117. end;
  118.  
  119.  
  120. { ********************************** }
  121. { ************  Globals ************ }
  122. { ********************************** }
  123.  
  124. { Abort the program and give a message }
  125.  
  126. procedure Abort(Msg: String);
  127. begin
  128.   Writeln;
  129.   Writeln(Msg);
  130.   Writeln('Program aborting');
  131.   Halt(1);
  132. end;
  133.  
  134. { Put the system into graphics mode }
  135.  
  136. procedure StartGraphics;
  137. var
  138.   Driver, Mode: Integer;
  139. begin
  140.   Driver := Detect;
  141.   InitGraph(Driver, Mode, PathToDrivers);
  142.   if GraphResult <> GrOK then
  143.   begin
  144.     Writeln(GraphErrorMsg(Driver));
  145.     if Driver = grFileNotFound then
  146.     begin
  147.       Writeln('in ', PathToDrivers,
  148.         '. Modify this program''s "PathToDrivers"');
  149.       Writeln('constant to specify the actual location of this file.');
  150.       Writeln;
  151.     end;
  152.     Writeln('Press Enter...');
  153.     Readln;
  154.     Halt(1);
  155.   end;
  156. end;
  157.  
  158. { Use the ForEach iterator to traverse and
  159.   show all the collection of graphical objects.
  160. }
  161.  
  162. procedure DrawAll(C: PCollection);
  163.  
  164. { Nested, far procedure. Receives one
  165.   collection element--a GraphObject, and
  166.   calls that elements Draw method.
  167. }
  168.  
  169. procedure CallDraw(P : PGraphObject); far;
  170. begin
  171.   P^.Draw;                            { Call Draw method }
  172. end;
  173.  
  174. begin { DrawAll }
  175.   C^.ForEach(@CallDraw);              { Draw each object }
  176. end;
  177.  
  178. { Instantiate and draw a collection of objects }
  179.  
  180. procedure MakeCollection(var List: PCollection);
  181. var
  182.   I: Integer;
  183.   P: PGraphObject;
  184. begin
  185.   { Initialize collection to hold 10 elements first, then grow by 5's }
  186.   List := New(PCollection, Init(10, 5));
  187.  
  188.   for I := 1 to 20 do
  189.   begin
  190.     case I mod 3 of                      { Create it }
  191.       0: P := New(PGraphPoint, Init);
  192.       1: P := New(PGraphCircle, Init);
  193.       2: P := New(PGraphRect, Init);
  194.     end;
  195.     List^.Insert(P);                     { Add it to collection }
  196.   end;
  197. end;
  198.  
  199. { ********************************** }
  200. { **********  Main Program ********* }
  201. { ********************************** }
  202.  
  203. var
  204.   GraphicsList: PCollection;
  205. begin
  206.   StartGraphics;                       { Activate graphics }
  207.  
  208.   MakeCollection(GraphicsList);        { Generate and collect figures }
  209.   DrawAll(GraphicsList);               { Use iterator to draw all }
  210.   Readln;                              { Pause to view figures }
  211.  
  212.   { Clean up }
  213.   Dispose(GraphicsList, Done);         { Delete collection }
  214.   CloseGraph;                          { Shut down graphics }
  215. end.
  216.